package com.softer.chengxin.param.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.config.CourseException;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.param.dao.ScoreTypeDao;
import com.softer.chengxin.param.entity.ScoreType;
import com.softer.chengxin.param.entity.ScoreTypeDetail;
import com.softer.chengxin.param.service.ScoreTypeService;
import com.softer.chengxin.score.dao.ScoreDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/12/13
 */
@Service
public class ScoreTypeServiceImpl implements ScoreTypeService {
    @Autowired
    private ScoreTypeDao scoreTypeDao;
    @Autowired
    private ScoreDetailDao scoreDetailDao;

    /**
     * 新增评分类型
     *
     * @param list 类型集合
     * @return 新增结果
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public CommonResult add(List<ScoreType> list) {
        for (ScoreType scoreType : list) {
            //默认开启
            scoreType.setState(NumberEnum.ONE.getNum());
            int num = scoreTypeDao.add(scoreType);
            if (num < 1) {
                throw new CourseException(ResultEnum.FAIL.getCode(), "新增评分类型失败");
            }
            List<ScoreTypeDetail> scoreList = scoreType.getScoreList();
            if (!scoreList.isEmpty()) {
                for (ScoreTypeDetail detail : scoreList) {
                    detail.setScoreTypeId(scoreType.getId());
                    int count = scoreTypeDao.addScoreTypeDetail(detail);
                    if (count < 1) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "新增分数类型失败");
                    }
                }
            }
        }
        return CommonResult.response(NumberEnum.ONE.getNum());
    }

    /**
     * 删除评分类型
     *
     * @param id ID
     * @return 删除结果
     */
    @Override
    public CommonResult delete(int id) {
        int num = scoreTypeDao.delete(id);
        return CommonResult.response(num);
    }

    /**
     * 修改评分类型
     *
     * @param scoreType 评分类型
     * @return 新增结果
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public CommonResult update(ScoreType scoreType) {
        int num = scoreTypeDao.update(scoreType);
        if (num == 1) {
            List<ScoreTypeDetail> scoreList = scoreType.getScoreList();
            if (!scoreList.isEmpty()) {
                for (ScoreTypeDetail detail : scoreList) {
                    detail.setScoreTypeId(scoreType.getId());
                    int count = scoreTypeDao.updateScoreTypeDetail(detail);
                    if (count < 1) {
                        throw new CourseException(ResultEnum.FAIL.getCode(), "修改分数类型失败");
                    }
                }
            }
            return CommonResult.response(NumberEnum.ONE.getNum());
        }
        return CommonResult.response(NumberEnum.ZERO.getNum());
    }

    /**
     * 删除分数类型
     *
     * @param id ID
     * @return 删除结果
     */
    @Override
    public CommonResult deleteScoreTypeDetail(int id) {
        int typeDetailNum = scoreDetailDao.getScoreTypeDetailNum(id);
        if (typeDetailNum> NumberEnum.ZERO.getNum()){
            return CommonResult.response(NumberEnum.ZERO.getNum(),"分数类型已经被使用，禁止删除");
        }
        int num = scoreTypeDao.deleteScoreTypeDetail(id);
        return CommonResult.response(num);
    }

    /**
     * 查询评分类型
     *
     * @param id 类型ID
     * @return 详情
     */
    @Override
    public ScoreType getInfo(int id) {
        ScoreType scoreType = scoreTypeDao.getScoreType(id);
        if (scoreType != null) {
            List<ScoreTypeDetail> scoreTypeDetailList = scoreTypeDao.getScoreTypeDetailList(id);
            scoreType.setScoreList(scoreTypeDetailList);
        } else {
            scoreType = new ScoreType();
        }
        return scoreType;
    }

    /**
     * 查询评分类型列表
     *
     * @param scoreType 查询实体
     * @param page      分页实体
     * @return 评分类型列表
     */
    @Override
    public PageInfo<ScoreType> getScoreTypeList(ScoreType scoreType, Page page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<ScoreType> list = scoreTypeDao.getScoreTypeList(scoreType);
        for (ScoreType s : list) {
            List<ScoreTypeDetail> scoreTypeDetailList = scoreTypeDao.getScoreTypeDetailList(s.getId());
            s.setScoreList(scoreTypeDetailList);
            //查询当前类型是否可删，该类型如果被使用过，则不可删
            int num = scoreTypeDao.getScoreTypeNum(s.getId());
            s.setDeleteFlag(num);
        }
        return new PageInfo<>(list);
    }

    /**
     * 修改评分类型状态
     *
     * @param id    评分类型ID
     * @param state 状态（0关闭，1开启）
     * @return 修改结果
     */
    @Override
    public CommonResult updateState(int id, int state) {
        int num = scoreTypeDao.updateState(id, state);
        return CommonResult.response(num);
    }

    /**
     * 获取评分类型下拉列表
     *
     * @param type 类型（1优良，2负面，3严重失信）
     * @return 评分类型下拉列表
     */
    @Override
    public List<ScoreType> getTypeList(int type) {
        return scoreTypeDao.getTypeList(type);
    }
    /**
     * 获取
     * 未关闭的
     * 评分类型下拉列表
     *
     * @param type 类型（1优良，2负面，3严重失信）
     * @return 评分类型下拉列表
     */
    @Override
    public List<ScoreType> getOpenTypeList(int type) {
        return scoreTypeDao.getOpenTypeList(type);
    }

    /**
     * 获取分数下拉列表
     *
     * @param id 评分类型ID
     * @return 分数下拉列表
     */
    @Override
    public List<ScoreTypeDetail> getScoreList(int id) {
        return scoreTypeDao.getScoreTypeDetailList(id);
    }
}
